﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;

namespace DBLibrary.Model.pojo
{
    public class Immediate : INotifyPropertyChanged ,IDisposable //即时数据表
    {

        public event PropertyChangedEventHandler PropertyChanged = delegate { };
        private int _BrachAdressId;                 //[分站地址]
        private int _SensorId;                      //[系统序号]
        private string _sensorName ;                     //[传感器名称态]
        private int _isValid;                       //[有效否]
        private float _currentValue;                //[当前值]
        private float _maxValue;                    //[最大值]
        private float _minValue;                    //[最小值]
        private float _averageValue;                //[平均值]
        private int _isWarning;                     //[报警否]
        private int _isOutage;                     //[断电否]
        private int _isOffline;                     //[断线否]
        private int _isError;                     //[故障否]
        private int _isFeedError;                     //[馈电异常否]
        private int _status=20;                     //[传感器状态]
        private DateTime _dateTime;                 //[时间]
        private int _listId;                        //[列表序号]
        private float _monthValue;                  //[当月累计值]
        private int _monthCount;                    //[当月累计次数]
        private int _systemId=0;                      //[system_id] 借用保存馈电异常检测的次数，赵金东
        private DateTime _alarmStartTime;           //[报警开始时间]
        private DateTime _outageStartTime;           //[断电开始时间]
        private DateTime _offlineStartTime;           //[断线开始时间]
        private DateTime _errorStartTime;           //[断电开始时间]
        private DateTime _feedErrorStartTime;           //[断电开始时间]
        private int _isUpload;                      //[上传否]
        private int _isUpload2;                     //[上传否2]
        private int _upState;                       //[UpState] 借用保存报警最大值时刻-报警开始时刻的秒数
        private DateTime _verifyTime;               //[标校时间]
        private int _alarmLevel;                    //[分级报警级别]
        private string _safeGuards;                 //[安全措施]
        private string _warningMessage;             //[报警信息]
        private string _installPlace; //安装地点
        private string _unit;//模拟量数值单位
        private int sensorType;//传感器类型，用于过滤
        private Dictionary<string, int> _feedErrorPort=new Dictionary<string, int>();//馈电有问题的端口，馈电异常计数
        private float? countValue;
        private float? countMaxValue;
        private float? countMinValue;
        private int? countNumber=0;
        private DateTime? countTime;
        private long alarmLevelTime1; //超分级报警值持续时间1
        private long alarmLevelTime2; //超分级报警值持续时间2
        private long alarmLevelTime3; //超分级报警值持续时间3
        private long alarmLevelTime4; //超分级报警值持续时间4
        private string customName; //组合编号 ：站号-端口号
        private string displayValue;//列表显示值
        private string openLabel;
        private string stopLabel;
        DateTime lastStatTime;//上一状态更新时间
        bool isCalibrating=false;
        private DateTime emergyWarningStartTime;           //[逻辑报警开始时间]

        bool logicAlarm = false;
        private int round = 0;
        private string custWarning;
        private int voltageChanged;//电池电压是否改变
        private int dataStatus = 200;
        private DateTime maxValueTime ;
        private DateTime minValueTime ;
        private float showRealValue;
        private DateTime recordTime;
        private string aIValue;

        public DateTime? DisplayAlarmStartTime
        {
            get {
                if (IsWarning == 1)
                    return _alarmStartTime;
                else
                    return null;
            }
        }
        public int BrachAdressId { get => _BrachAdressId; 
            set { _BrachAdressId = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged.Invoke(this, new PropertyChangedEventArgs("BrachAdressId"));
                }
            } 
        }
        public string SafeGuards { get => _safeGuards;
            set
            {
                _safeGuards = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged.Invoke(this, new PropertyChangedEventArgs("SafeGuards"));
                }
            }
        }
        public float CurrentValue { get => _currentValue;
            set
            {
                _currentValue = value;
                if (value == -999999)
                    Status = 2;
                if (PropertyChanged != null)
                {
                    PropertyChanged.Invoke(this, new PropertyChangedEventArgs("CurrentValue"));
                    PropertyChanged.Invoke(this, new PropertyChangedEventArgs("DisplayValue"));
                }
            }
        }
        public DateTime DateTime { get => _dateTime;
            set
            {
                _dateTime = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged.Invoke(this, new PropertyChangedEventArgs("DateTime"));
                    if (LastStatTime == new DateTime(0))
                    {
                        LastStatTime = value;
                        PropertyChanged.Invoke(this, new PropertyChangedEventArgs("LastStatTime"));
                    }
                }
            }
        }
        public int SensorId { get => _SensorId;
            set
            {
                _SensorId = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged.Invoke(this, new PropertyChangedEventArgs("SensorId"));
                }
            }
        }
        public float MaxValue { get => _maxValue;
            set
            {
                _maxValue = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged.Invoke(this, new PropertyChangedEventArgs("MaxValue"));
                }
            }
        }
        public float MinValue { get => _minValue;
            set
            {
                _minValue = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged.Invoke(this, new PropertyChangedEventArgs("MinValue"));
                }
            }
        }
        public float AverageValue { get => _averageValue;
            set
            {
                _averageValue = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged.Invoke(this, new PropertyChangedEventArgs("AverageValue"));
                }
            }
        }
        public int IsValid { get => _isValid;
            set
            {
                _isValid = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged.Invoke(this, new PropertyChangedEventArgs("IsValid"));
                }
            }
        }
        public int IsWarning { get => _isWarning;
            set
            {
                _isWarning = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged.Invoke(this, new PropertyChangedEventArgs("IsWarning"));
                    PropertyChanged.Invoke(this, new PropertyChangedEventArgs("DisplayAlarmStartTime"));
                }
            }
        }
        public int Status {
            get
            {
                return _status;
            }
            set
            {
                _status = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Status"));
                    PropertyChanged.Invoke(this, new PropertyChangedEventArgs("DisplayValue"));
                }
            }
        }
        public int ListId { get => _listId;
            set
            {
                _listId = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged.Invoke(this, new PropertyChangedEventArgs("ListId"));
                }
            }
        }
        public float MonthValue { get => _monthValue;
            set
            {
                _monthValue = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged.Invoke(this, new PropertyChangedEventArgs("MonthValue"));
                }
            }
        }
        public int MonthCount { get => _monthCount;
            set
            {
                _monthCount = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged.Invoke(this, new PropertyChangedEventArgs("MonthCount"));
                }
            }
        }
        public int SystemId { get => _systemId;
            set
            {
                _systemId = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged.Invoke(this, new PropertyChangedEventArgs("SystemId"));
                }
            }
        }
        public DateTime AlarmStartTime { get => _alarmStartTime;
            set
            {
                _alarmStartTime = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged.Invoke(this, new PropertyChangedEventArgs("AlarmStartTime"));
                    PropertyChanged.Invoke(this, new PropertyChangedEventArgs("DisplayAlarmStartTime"));
                }
            }
        }
        public int IsUpload { get => _isUpload;
            set
            {
                _isUpload = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged.Invoke(this, new PropertyChangedEventArgs("IsUpload"));
                }
            }
        }
        public int IsUpload2 { get => _isUpload2;
            set
            {
                _isUpload2 = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged.Invoke(this, new PropertyChangedEventArgs("IsUpload2"));
                }
            }
        }
        public int UpState { 
            get {
                if (_upState.ToString() == "")
                    return 0;
                else
                    return _upState;
            }
            set
            {
                _upState = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged.Invoke(this, new PropertyChangedEventArgs("UpState"));
                }
            }
        }
        public DateTime VerifyTime { get => _verifyTime;
            set
            {
                _verifyTime = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged.Invoke(this, new PropertyChangedEventArgs("VerifyTime"));
                }
            }
        }
        public int AlarmLevel { get => _alarmLevel;
            set
            {
                _alarmLevel = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged.Invoke(this, new PropertyChangedEventArgs("AlarmLevel"));
                }
            }
        }
        public string WarningMessage {
            get
            {
                 return _warningMessage;
            }
            set
            {
                _warningMessage = value;
                custWarning = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged.Invoke(this, new PropertyChangedEventArgs("WarningMessage"));
                    PropertyChanged.Invoke(this, new PropertyChangedEventArgs("CustWarning"));
                }
            }
        }

        public string SensorName { get => _sensorName; set => _sensorName = value; }
        public Dictionary<string, int> FeedErrorPort { get => _feedErrorPort; set => _feedErrorPort = value; }
        public float? CountValue { get => countValue; set => countValue = value; }
        public int? CountNumber { get => countNumber; set => countNumber = value; }
        public DateTime? CountTime { get => countTime; set => countTime = value; }
        public float? CountMaxValue { get => countMaxValue; set => countMaxValue = value; }
        public float? CountMinValue { get => countMinValue; set => countMinValue = value; }

        public long AlarmLevelTime1 { get => alarmLevelTime1; set => alarmLevelTime1 = value; }
        public long AlarmLevelTime2 { get => alarmLevelTime2; set => alarmLevelTime2 = value; }
        public long AlarmLevelTime3 { get => alarmLevelTime3; set => alarmLevelTime3 = value; }
        public long AlarmLevelTime4 { get => alarmLevelTime4; set => alarmLevelTime4 = value; }
        public string CustomName { get => customName; set => customName = value; }
        public int CustomNameInt
        {
            get {
                string[] s = customName.Split('-');
                return int.Parse(s[0] + s[1].PadLeft(2,'0'));
            }
        }
        public DateTime OutageStartTime { 
            get => _outageStartTime;
            set { 
                _outageStartTime = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged.Invoke(this, new PropertyChangedEventArgs("OutageStartTime"));
                }
            }
        }
        public DateTime OfflineStartTime { 
            get => _offlineStartTime;
            set { 
                _offlineStartTime = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged.Invoke(this, new PropertyChangedEventArgs("OfflineStartTime"));
                }
            } 
        }
        public DateTime ErrorStartTime { 
            get => _errorStartTime;
            set { 
                _errorStartTime = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged.Invoke(this, new PropertyChangedEventArgs("ErrorStartTime"));
                }
            }
        }
        public DateTime FeedErrorStartTime
        {
            get => _feedErrorStartTime;
            set
            {
                _feedErrorStartTime = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged.Invoke(this, new PropertyChangedEventArgs("FeedErrorStartTime"));
                }
            }
        }
        public int IsOutage { get => _isOutage; set => _isOutage = value; }
        public int IsOffline { 
            get {
                if (_isOffline == 1)
                    return _isOffline;
                else if (_status == 2)
                    return 1;
                else
                    return 0;

            } 
            set => _isOffline = value; }
        public int IsError { get => _isError; set => _isError = value; }
        public int IsFeedError { get => _isFeedError; set => _isFeedError = value; }
        public string DisplayValue {
            set { }
            get {
                if (Status == 2 || Status >=9||CurrentValue<-8888 || ShowRealValue < -88888)
                    return "--";
                if (openLabel==null ||openLabel == string.Empty)
                    return ShowRealValue.ToString()+Unit;
                else
                {
                    if (ShowRealValue == 0)
                        return StopLabel;
                    else
                        return OpenLabel;
                }
            }
            
        }

        public string OpenLabel { get => openLabel; set => openLabel = value; }
        public string StopLabel { get => stopLabel; set => stopLabel = value; }
        public string InstallPlace { get => _installPlace; set => _installPlace = value; }
        public string Unit { get => _unit; set => _unit = value; }
        public DateTime LastStatTime { get => lastStatTime; set => lastStatTime = value; }
        public bool IsCalibrating {
            get => isCalibrating;
            set
            {
                isCalibrating = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged.Invoke(this, new PropertyChangedEventArgs("IsCalibrating"));
                }
            }
        }
        public DateTime EmergyWarningStartTime { get => emergyWarningStartTime; set => emergyWarningStartTime = value; }
        public bool LogicAlarm { get => logicAlarm; set => logicAlarm = value; }
        public string ChartName { 
            get
            {
                return "Chart" + SensorId.ToString();
            }
        }

        public int Round { 
            get => round; 
            set {
                round = value;
                if(value>2)
                    CustWarning = _warningMessage;
                if (PropertyChanged != null)
                {
                    PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Round"));
                }
            }
        }

        public string CustWarning { 
            get {
                if (Status == 11)
                    return "分站断线";
                else if (Status == 0)
                    return "";
                else
                    return WarningMessage;
            }
            set {
                custWarning=value;
                if (PropertyChanged != null)
                {
                    PropertyChanged.Invoke(this, new PropertyChangedEventArgs("CustWarning"));
                }
            }
        }

        public int VoltageChanged { get => voltageChanged; set => voltageChanged = value; }
        public float ShowRealValue
        {
            get => showRealValue;
            set
            { 
                showRealValue = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged.Invoke(this, new PropertyChangedEventArgs("ShowRealValue"));
                    PropertyChanged.Invoke(this, new PropertyChangedEventArgs("DisplayValue"));
                }
            }
        }

        public int DataStatus {
            get => dataStatus;
            set
            {
                dataStatus = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged.Invoke(this, new PropertyChangedEventArgs("DataStatus"));
                }
            }
        }
        public DateTime MaxValueTime {
            get => maxValueTime;
            set
            {
                maxValueTime = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged.Invoke(this, new PropertyChangedEventArgs("MaxValueTime"));
                }
            }
        }
        public DateTime MinValueTime {
            get => minValueTime;
            set
            {
                minValueTime = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged.Invoke(this, new PropertyChangedEventArgs("MinValueTime"));
                }
            }
        }
        public int SensorType { get => sensorType; set => sensorType = value; }
        public DateTime RecordTime { get => recordTime; set => recordTime = value; }
        public string AIValue { get => aIValue; 
            set {     
                aIValue = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged.Invoke(this, new PropertyChangedEventArgs("AIValue"));
                }
            }
  
        }

        public void AddData(float data)
        {

        }

        public void Dispose()
        {
            GC.SuppressFinalize(this);
        }
    }
}
